package com.ywb.edugui.dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.ywb.edugui.dao.SubjectDao;
import com.ywb.edugui.entity.Subject;
import com.ywb.edugui.entity.vo.FirstSubject;
import com.ywb.edugui.entity.vo.SecondSubject;
import com.ywb.edugui.utils.MySnowflakeIdWorker;
import com.ywb.edugui.utils.SnowflakeIdWorker;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class SubjectDaoImpl extends BaseDaoImpl<Subject> implements SubjectDao {
    List<Subject> batchList = null;

    @Override
    public List<Subject> list() {
        String sql = "select *  from subject";
        List<Subject> list = list(sql);
        return list;
    }
    @Override
    public List<FirstSubject> firstSubjectList(){
        String sql = "select *  from subject";
        List<Subject> list = list(sql);
//        将查找出的数据封装到vo中
        List<FirstSubject> parents = list.stream()
                .filter(subject ->
//                使用过滤器找到所有FirstSubject
                        subject.getParent_id().equals("0"))
                .map(subject -> {
                    FirstSubject firstSubject = new FirstSubject();
                    firstSubject.setId(subject.getId());
                    firstSubject.setTitle(subject.getTitle());
                    List<SecondSubject> chidren = list.stream()
                            .filter(subject1 -> //遍历list中parent_id为当前subject的id的科目作为二级科目
                                    subject1.getParent_id().equals(subject.getId()))
                            .map(subject1 ->{
                                SecondSubject secondSubject = new SecondSubject(subject1.getId() , subject1.getTitle());
                                return secondSubject;
                            })
                            .collect(Collectors.toList());
                    firstSubject.setChildren(chidren);
                    return firstSubject;
                })
                .collect(Collectors.toList());
        return parents;
    }

    @Override
    public Subject getByTitle(String title) {
        String sql = "select * from subject where title = ?";
        return getOne(sql, title);
    }

    @Override
    public int add(Subject subject) {
        if(StringUtils.isEmpty(subject.getId())){
            subject.setId(MySnowflakeIdWorker.nextId());
        }
        String sql = "insert into subject(id, title, parent_id) values(?, ?, ?)";
        return update(sql, subject.getId(), subject.getTitle(), subject.getParent_id());
    }
    @Override
    public void addBatchParams(Subject subject){
        if(batchList == null) {
            batchList = new ArrayList<>();
        }

        if(StringUtils.isEmpty(subject.getId())){
            subject.setId(SnowflakeIdWorker.nextId());
        }

        batchList.add(subject);
    }
    @Override
    public int[] addBatch(){
        if(batchList == null){
            return null;
        }
        Object[][] params = batchList.stream().map(subject -> {
            Object[] object = {subject.getId(), subject.getTitle(), subject.getParent_id()};
            return object;
        }).toArray(Object[][]::new);

        String sql = "insert into subject(id, title, parent_id) values(?, ?, ?)";
        return batch(sql, params);
    }
    @Override
    public int delete(Subject subject) {
        String sql = "delete from subject where id = ?";
        return update(sql, subject.getId());
    }

    @Override
    public int delete(String id) {
        String sql = "delete from subject where id = ?";
        return update(sql, id);
    }

    @Override
    public int update(Subject subject) {
        String sql = "update subject set title = ?, parent_id = ? where id = ?";
        return update(sql, subject.getTitle(), subject.getParent_id(), subject.getId());
    }

    @Override
    public Subject getOne(String id) {
        String sql = "select * from subject where id = ?";
        return getOne(sql, id);
    }
}
